
DECLARE @v_TableName VARCHAR (100)
DECLARE @v_Table TABLE (Table_Name VARCHAR (100))
--DECLARE @v_TableSize TABLE (
--            Table_Name VARCHAR(100)
--           ,ROWS BIGINT
--           ,Reserved VARCHAR(50)
--           ,ActualDataSize VARCHAR(50)
--           ,IndexSize VARCHAR(50)
--           ,Unused VARCHAR(50)
--        )

--INSERT INTO @v_Table
--SELECT Table_Name
--FROM   Information_Schema.Tables
--WHERE  Table_Type = 'BASE TABLE'

--SELECT @v_TableName = Table_Name
--FROM   @v_Table

--WHILE EXISTS (
--          SELECT 1
--          FROM   @v_Table
--      )
--BEGIN
DECLARE @v_TableSize TABLE (
            Table_Name VARCHAR (100), ROWS BIGINT, Reserved VARCHAR (50), ActualDataSize VARCHAR (50), 
            IndexSize VARCHAR (50), Unused VARCHAR (50)
        )

INSERT INTO @v_TableSize
EXEC sys.sp_MSforeachtable 'sp_spaceused ''?'''

--SELECT *
--FROM   @v_TableSize
--    DELETE 
--    FROM   @v_Table
--    WHERE  Table_Name = @v_TableName
    
--    SELECT @v_TableName = Table_Name
--    FROM   @v_Table
--END
SELECT * 
FROM   @v_TableSize t
ORDER BY
       cast(REPLACE (t.Reserved,'KB','') AS INT)+ 
       cast(REPLACE (t.ActualDataSize,'KB','') AS INT)+
       cast(REPLACE (t.IndexSize ,'KB','') AS INT)+
       cast(REPLACE (t.Unused,'KB','') AS INT)
DESC
       
GO
